<?php

/**
 * Created on February 7, 2011
 * @Author Mike Tierney
 * This is the view file.
 * It controls the internship file to manipulate data
 *
 */
echo "<html><body>";
include '../db-control/inc/functions.php';
include '../model/Internship.php';
include '../db-control/dbInternship.php';

//update a internship if the user is trying to do so
if (isset($_POST['update']) && $_POST['update'] != '') {
    $id = $_POST['update'];
    $i = new Internship();        //creates a new instance of Internship
    $i->setID($id);  //sets the internship ID
    $db = new dbInternship();
    $exists = $db->checkID($i); //checks to see if the ID is a real contact
   if (!$exists) {
        echo "<b>Internship with ID '$id' does not exist...</b>";
    } else {
        $i->setSemester($_POST['semester']); //sets the internship semester
        $i->setHours($_POST['hours']); //sets the internship hours
        $result = $db->updateInternship($i); //finally, updates the internship with the data we've set
        if (!$result) {
            echo "<b>Could not update user with ID '$id'...</b>";
        }
    }
} else {
    if (isset($_POST['add'])) {
        $i = new Internship();
        $i->setSemester($_POST['semester']);
        $i->setHours($_POST['hours']);
        $db = new dbInternship();
        $result = $db->addInternship($i);
        if (!$result) {
            echo "<b>Please enter some internship information</b>";
        }
    }

    if (isset($_POST['deleteButton'])) {
        $i = new Internship(); //creates a new Internship object (i.e. creates a new internship)
        $i->setID($_POST['deleteID']);
        $db = new dbInternship();
        $result = $db->deleteInternship($i);
        if (!$result) {
            echo "<b>Could not delete internship with ID '$_POST[deleteID]'...</b>";
        }
    }
}


//displays the web form used for adding a new internships
echo <<<_FORM


<form action='viewInternship.php' method='post'>
<table>
    <tr><td colspan='2'>Specify an ID to change an Internship's data</td><td><input type='text' size='4' name='update' /></td></tr>
    <tr><td>Semester:</td><td><input type='text' value='' name='semester' /></td></tr>
    <tr><td>Hours:</td><td><input type='text' value='' name='hours' /></td></tr>
    <tr><td></td><td align='right'><input type='submit' value='Add Internship' name='add'/></td></tr>
</table>
</form>


_FORM;

displayInternships(); //displays the internship list


echo "</body></html>";

function displayInternships() {
    $db = new dbInternship(); //gets all the internshipss from the database
    $result = $db->getAllInternships();

    echo "<table width='200px'>";
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $id = $row['id'];
        $semester = $row['semester'];
        $hours = $row['hours'];
        echo "<tr><td>$id</td><td></td></tr>";
        echo "<tr><td align='right'>Semester:</td><td align='left'>$semester</td></tr>";
        echo "<tr><td align='right'>Hours:</td><td align='left'>$hours</td></tr>";
        echo "<tr><td colspan='2' align='right'><form action='viewInternship.php' method='post'><input type='hidden' value='$id' name='deleteID' /><input type='submit' value='Delete' name='deleteButton' /></form></td><td></td></tr>";
        echo "<tr><td colspan='2'><hr /></td><td></td></tr>";
    }
    echo "</table>";
}
?>


   